package com.manner.groupon.controller.store;

import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.PageResult;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.model.dto.search.StoreRequest;
import com.manner.groupon.model.dto.search.StoreSearch;
import com.manner.groupon.model.dto.shop.ChargeOffGrouponCouponRequest;
import com.manner.groupon.model.dto.store.StoreSaveDTO;
import com.manner.groupon.model.entity.Store;
import com.manner.groupon.model.vo.*;
import com.manner.groupon.service.store.StoreLinkManService;
import com.manner.groupon.service.store.StoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: Manner
 * @Description: 商家请求控制器
 * @DateTime: 2024/10/10 1:18
 */
@RestController
@RequestMapping("/store")
@RequiredArgsConstructor
public class StoreController {

    private final StoreService storeService;

    private final StoreLinkManService storeLinkManService;

   /**
    *
    * @description: 获取指定店铺在当前这个季度中每个月的订单数, 收益额, 退款额数据
    * @author Manner
    * @date 2024/12/11 2:25
    * @param storeNo 店铺编号
    * @return BaseResponse<HashMap<List<Long>>>
    */
    @GetMapping("/quarter-every-month/{storeNo}")
    public BaseResponse<HashMap<String, List<String>>> loadQuarterEveryMonthData(@PathVariable String storeNo) {
        return ResultUtils.success(storeService.loadQuarterEveryMonthInformation(storeNo));
    }

    /**
     *
     * @description: 获取指定店铺在当前这个月中每个星期的订单数, 收益额, 退款额数据 ,以及每周的环比数据
     * @author Manner
     * @date 2024/12/11 2:24
     * @param storeNo 店铺编号
     * @return BaseResponse<HashMap<List<Long>>>
     */
    @GetMapping("/month-every-week/{storeNo}")
    public BaseResponse<StoreWeekDataAssayVO> loadMonthEveryWeekData(@PathVariable String storeNo) {
        return ResultUtils.success(storeService.loadMonthEveryWeekInformation(storeNo));
    }

    /**
     * @param storeNo 店铺编号
     * @return BaseResponse<HashMap < List < Long>>>
     * @description: 获取店铺这个星期每天的订单数, 收益额, 退款额数据
     * @author Manner
     * @date 2024/12/10 22:31
     */
    @GetMapping("/week-every-day/{storeNo}")
    public BaseResponse<HashMap<String, List<Long>>> loadWeekEveryDayData(@PathVariable String storeNo) {
        return ResultUtils.success(storeService.loadWeekEveryDayInformation(storeNo));
    }

    /**
     * @param storeNo 店铺编号
     * @return BaseResponse<StoreBusinessDataSummaryAssayVO>
     * @description: 店铺门店数据汇总分析
     * @author Manner
     * @date 2024/12/10 17:44
     */
    @GetMapping("/business-data-summary/{storeNo}")
    public BaseResponse<StoreIncomeDataSummaryAssayVO> loadStoreBusinessDataSummary(@PathVariable String storeNo) {
        return ResultUtils.success(storeService.loadStoreBusinessDataSummaryAssay(storeNo));
    }

    /**
     * @param storeNo 店铺编号
     * @return BaseResponse<StoreHomeDataAssayVO>
     * @description: 商家后台主页数据展示分析
     * @-author Manner
     * @date 2024/12/9 0:50
     */
    @GetMapping("/home-data-assay/{storeNo}")
    public BaseResponse<StoreHomeDataAssayVO> loadStoreHomeDataAssay(@PathVariable String storeNo) {
        return ResultUtils.success(storeService.getStoreHomeDataAssay(storeNo));
    }

    /**
     * @param phone 登录后台的门店联系人手机账号
     * @return BaseResponse<LinkmanLoadStoreListVo>
     * @description: 根据手机号获取门店联系人名下的一个店铺信息
     * @author Manner
     * @date 2024/12/8 23:45
     */
    @GetMapping("/one/{phone}")
    public BaseResponse<LinkmanLoadStoreListVo> loadOneStore(@PathVariable String phone) {
        return ResultUtils.success(storeService.getLinkmanOneStore(phone));
    }

    /**
     * @param phone 登录后台的门店联系人手机账号
     * @return BaseResponse<List < LinkmanLoadStoreListVo>>
     * @description: 根据手机号获取门店联系人名下的所有店铺信息
     * @author Manner
     * @date 2024/12/8 22:11
     */
    @GetMapping("/list/{phone}")
    public BaseResponse<List<LinkmanLoadStoreListVo>> loadStoreList(@PathVariable String phone) {
        return ResultUtils.success(storeService.getLinkmanAllStore(phone));
    }

    /**
     * 商家核销团购优惠券
     *
     * @param requestArgs
     * @return BaseResponse<Boolean>
     * @author Manner
     * @date 2024/10/13 16:56
     */
    @PutMapping("/coupon/charge")
    public BaseResponse<String> chargeOff(@RequestBody ChargeOffGrouponCouponRequest requestArgs) {
        return storeService.chargeOffGrouponCoupons(requestArgs);
    }

    @GetMapping("/search")
    public BaseResponse<List<Store>> list() {
        List<Store> list = storeService.list();
        return ResultUtils.success(list);
    }

    @GetMapping
    public BaseResponse<PageResult<StoreVo>> list(StoreSearch storeSearch) {
        return storeService.search(storeSearch);
    }

    @GetMapping("/{no}")
    public BaseResponse<StoreVo> queryByNo(@PathVariable String no) {
        return storeService.queryByNo(no);
    }

    @GetMapping("/admin")
    public BaseResponse<PageResult<AdminStoreVO>> search(StoreRequest storeRequest) {
        return storeService.query_search(storeRequest);
    }

    @GetMapping("/phone/{phone}")
    public BaseResponse<List<Store>> search(@PathVariable String phone) {
        return storeService.query_phone(phone);
    }

    @GetMapping("/user/{phone}")
    public BaseResponse<Store> queryByPhone(@PathVariable String phone) {
        List<Store> list = storeService.query().eq("admin_id", storeLinkManService.query().eq("phone", phone).one().getId()).list();
        return ResultUtils.success(list.get(0));
    }

    @PutMapping
    public BaseResponse<String> update(@RequestBody Store store) {
        return storeService.updateByNo(store);
    }

    @PutMapping("/{id}/{status}")
    public BaseResponse<String> update(@PathVariable Long id, @PathVariable Integer status) {
        return storeService.updateStatus(id, status);
    }

    @GetMapping("/admin/{id}")
    public BaseResponse<AdminStoreVO> queryById(@PathVariable Long id) {
        return storeService.query_id(id);
    }

    @PostMapping
    public BaseResponse<String> save(@RequestBody StoreSaveDTO storeSaveDTO) {
        return storeService.insert(storeSaveDTO);
    }

}